package com.fingard.dsp.bank.directbank.ybt11;

import java.sql.Date;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;

/**
 * 银保通批量代收付
 * 
 * @author fanghh
 * @date 2018/8/10
 */
public class YBT11BatPayRec extends YBT11Base implements IBatPayRec {
	@Override
	public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988) throws Exception {
		return d_BatPayRec(p_req1988);
	}

	@Override
	public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
		return d_QryBatPayRec(p_req1998);
	}

	@Override
	public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
		return d_BatPayRec(p_req9188);
	}

	@Override
	public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
		return d_QryBatPayRec(p_req9198);
	}
	
	/**
	 * 批量代收付批次提交
	 * 
	 * @param p_reqBat
	 * @return
	 * @throws Exception
	 */
	public RetAtsBatPayRec d_BatPayRec(ReqBatPayRec p_reqBat) throws Exception {
		RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
		
		try {
			while(p_reqBat.readRecord()) {
				String urid = UUID.randomUUID().toString().replaceAll("-", "");
				String payDirection = "";
				if ("1988".equals(p_reqBat.reqHeader.transType)) {
					payDirection = "1";
				} else {
					payDirection = "2";
				}
				String payType = getBankConfig().getMapValueByKey("payTypeMapping", p_reqBat.readedDetail.memo);
				String oppBankCode = "";
				if ("1988".equals(p_reqBat.reqHeader.transType)) {
					oppBankCode = p_reqBat.readedDetail.recBankCode;
				} else {
					oppBankCode = p_reqBat.readedDetail.payBankCode;
				}
				String cnapsCode = p_reqBat.readedDetail.cnapsCode;
				String ownAct = "";
				String ownArea = "";
				if ("1988".equals(p_reqBat.reqHeader.transType)) {
					ownAct = p_reqBat.readedDetail.payAct;
					ownArea = p_reqBat.readedDetail.payArea;
				} else {
					ownAct = p_reqBat.readedDetail.recAct;
					ownArea = p_reqBat.readedDetail.recArea;
				}
				String oppAct = "";
				String oppName = "";
				if ("1988".equals(p_reqBat.reqHeader.transType)) {
					oppAct = p_reqBat.readedDetail.recAct;
					oppName = p_reqBat.readedDetail.recName;
				} else {
					oppAct = p_reqBat.readedDetail.payAct;
					oppName = p_reqBat.readedDetail.payName;
				}
				String payAmount = p_reqBat.readedDetail.payAmount;
				String isPrivate = "";
				if ("1".equals(p_reqBat.readedDetail.isPrivate)) {
					isPrivate = "1";
				} else {
					isPrivate = "2";
				}
				String payPeriod = "";
				if ("9188".equals(p_reqBat.reqHeader.transType)) {
					payPeriod = p_reqBat.readedDetail.useType;
				}
				String oppArea = "";
				if ("1988".equals(p_reqBat.reqHeader.transType)) {
					oppArea = p_reqBat.readedDetail.recArea;
				} else {
					oppArea = p_reqBat.readedDetail.payArea;
				}
				String cardType = "";
				if ("1".equals(p_reqBat.readedDetail.cardType)) {
					cardType = "1";
				} else if ("3".equals(p_reqBat.readedDetail.cardType)) {
					cardType = "1";
				} else {
					cardType = "2";
				}
				
				String certType = p_reqBat.readedDetail.certType;
				String certNum = p_reqBat.readedDetail.certNum;
				String cellPhone = p_reqBat.readedDetail.cellPhone;
				
				LinkedList<Object> params = new LinkedList<Object>();
				
				Date paySendDate = new Date(p_reqBat.getPayDateTime().getTime());
				params.add(urid);
				params.add(p_reqBat.reqBatchNO);
				params.add(p_reqBat.readedDetail.askNumber);
				params.add(p_reqBat.readedDetail.abstractATS);
				params.add(paySendDate);
				params.add(p_reqBat.readedDetail.sourceNoteCode);
				params.add(payDirection);
				params.add(payType);
				params.add(oppBankCode);
				params.add(ownArea);
				params.add(cnapsCode);
				params.add(ownAct);
				params.add(oppAct);
				params.add(oppName);
				params.add(payAmount);
				params.add(isPrivate);
				params.add(payPeriod);
				params.add(oppArea);
				params.add(cardType);
				params.add(certType);
				params.add(certNum);
				params.add(cellPhone);
				params.add("");
				params.add("");
				params.add("CNY");
				params.add("");
				params.add("");
				params.add("1");// 未支付
				params.add("");
				params.add("");
				params.add("");
				params.add("1");// 未取盘
				params.add("");
				params.add("1");// 未回盘
				params.add("");
				params.add(paySendDate);
				params.add("ATS");
				params.add(paySendDate);
				params.add("ATS");
				params.add("1");
				params.add("");
				params.add("");
				params.add("");
				params.add("");
				params.add("");
				
				StringBuilder sbLog = new StringBuilder();
				boolean saved = saveEachRecords(params, sbLog);
				if(!saved){
					throw new Exception(sbLog.toString());
				}
				
				retAts.respCode = "0";
				retAts.respInfo = "数据已插入中间表";
				retAts.reqSeqID = p_reqBat.reqBatchNO;
				retAts.transState = "YBT+inserted";
				retAts.payInfoCode = "inserted";
			}	
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		retAts.writeRetToATS();
		return retAts;
	}
	
	/**
	 * 批量代收付状态查询
	 * 
	 * @param p_reqQryBat
	 * @return
	 * @throws Exception
	 */
	public RetAtsQryBatPayRec d_QryBatPayRec(ReqQryBatPayRec p_reqQryBat) throws Exception {
		RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_reqQryBat.reqHeader);
		retAts.reqSeqID = p_reqQryBat.reqBatchNO;
		
		try {
			List<Map<String,Object>> tmpRespRecords = new ArrayList<Map<String,Object>>();
			tmpRespRecords = getResultRecords(p_reqQryBat.reqBatchNO);
			
			if(tmpRespRecords != null && tmpRespRecords.size() > 0) {
				retAts.respCode = "0";
				
	            for(Map<String,Object> eachRecord:tmpRespRecords) {
	                RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
					
				    tmpRetDetail.postScript = String.valueOf(eachRecord.get("ASKNUMBER"));
				    tmpRetDetail.payAmount = String.valueOf(eachRecord.get("PAYMONEY"));;
				    
				    if ("1998".equals(p_reqQryBat.reqHeader.transType)) {
				    	tmpRetDetail.recAct = String.valueOf(eachRecord.get("CUSACCOUNT"));
				    	tmpRetDetail.recName = String.valueOf(eachRecord.get("CUSNAME"));
				    } else {
				    	tmpRetDetail.payAct = String.valueOf(eachRecord.get("CUSACCOUNT"));
				    	tmpRetDetail.payName = String.valueOf(eachRecord.get("CUSNAME"));
				    }
					
					tmpRetDetail.result = "YBT+" + String.valueOf(eachRecord.get("PAYSTATE"));
					tmpRetDetail.payInfoCode = String.valueOf(eachRecord.get("PAYINFOCODE"));
					tmpRetDetail.memo = String.valueOf(eachRecord.get("PAYINFO"));
					
	        		retAts.writeRetDetail(tmpRetDetail);
	            }
	        } else {
	        	retAts.respCode = "-1";
				retAts.respInfo = "银行查询结果文件内容为空!";
				WriteBankLogLn2("没有符合条件的记录!");
	        }
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		retAts.writeAfterDetail();
		return retAts;
	}
}
